import 'package:flutter/material.dart';
import 'TextHelp.dart';

///@ClassName: FlutterPainter
/// @Author: guoyangyang
///@Date: 2022 1/19/22 5:51 PM
///@Description:

class GoodsLabelHelp extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    printTest(canvas);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  printTest(Canvas canvas) {
    const double left = 10;
    TextSize textStyle_12 = TextSize(mFontSize: 12);
    double height = left;
    //托寄物的高度
    double height_tuojiwu = 0;
//产品类型的高度
    double height_product = 0;
    //收派员的高度
    double height_person = 0;
    /// 初始化画笔
    var paint = Paint()
      ..strokeWidth = 1.0
      ..color = Colors.black;
    // TextPainter, 传入minWidth，maxWidth来限制它的宽度，如不传，文字会绘制在一行

    //第一行 Offset(startX,startY),Offset(endX,endY)
    canvas.drawLine(Offset(0, height), Offset(maxWidth_printer, height), paint);
    final text_1_1 = TextDraw( text: TextSpan(text: "客户存根", style: textStyle_12),)
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      //Offset(x,y)
      ..paint(canvas, Offset(30 * scale, height));
    height = text_1_1.height + 17 * scale;
//第2行
    final text_2_1 = TextDraw( text: TextSpan(text: "11/1 母单号 SF 100 252 888 9848", style: textStyle_12),)
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
    //Offset(x,y)
      ..paint(canvas, Offset(17 * scale, height));
    height = height + text_2_1.height;
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //寄件方
    final text_3_1 = TextDraw(
      text: TextSpan(text: "寄件方：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    //
    height = height + text_3_1.height;
    final text_4_1 = TextDraw(
      text: TextSpan(text: "邓木师 15238374040", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    //
    height = height + text_4_1.height;
    final text_5_1 = TextDraw(
      text: TextSpan(text: "广东省广州市越秀区白云路99号606", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    //
    height = height + text_4_1.height;
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //收件方
    final text_6_1 = TextDraw(
      text: TextSpan(text: "收件方：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    //
    height = height + text_6_1.height;
    final text_7_1 = TextDraw(
      text: TextSpan(text: "田小姐 138*****7777", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    //
    height = height + text_7_1.height;
    final text_8_1 = TextDraw(
      text: TextSpan(
          text: "中国，河南省，洛阳市，花果山，水帘洞，999号302室",
          style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));

    height = height + text_8_1.height;
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //托寄物
    height_tuojiwu = height;
    final text_9_1 = TextDraw(
      text: TextSpan(text: "托寄物：平板电脑一台", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    //费用合计
    final text_9_2 = TextDraw(
      text: TextSpan(text: "费用合计：19.00元", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer * 3 / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 4 + left, height));
    height = height + text_9_2.height;
    //运费
    final text_9_3 = TextDraw(
      text: TextSpan(text: "运费：12元", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer * 3 / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 4 + left, height));
    height = height + text_9_3.height;
    //保价
    final text_9_4 = TextDraw(
      text: TextSpan(text: "保价：7元", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer * 3 / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 4 + left, height));
    //
    height = height + text_9_1.height + 14 * scale;
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //产品类型
    height_product = height;
    final text_10_1 = TextDraw(
      text: TextSpan(text: "产品类型：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    final text_10_2 = TextDraw(
      text: TextSpan(text: "顺丰标快", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 4 + left, height));
    final text_10_3 = TextDraw(
      text: TextSpan(text: "件数：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 2 + left, height));
    final text_10_4 = TextDraw(
      text: TextSpan(text: "1", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer * 3 / 4 + left, height));
    //
    height = height + text_10_1.height + 10;
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //付款方式
    final text_11_1 = TextDraw(
      text: TextSpan(text: "付款方式：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    final text_11_2 = TextDraw(
      text: TextSpan(text: "寄付现结", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 4 + left, height));
    final text_11_3 = TextDraw(
      text: TextSpan(text: "计费重量：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 2 + left, height));
    final text_11_4 = TextDraw(
      text: TextSpan(text: "100KG", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer * 3 / 4 + left, height));
    //
    height = height + text_11_1.height + 10;
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //收派员
    height_person = height;
    final text_12_1 = TextDraw(
      text: TextSpan(text: "收派员：", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(left, height));
    final text_12_2 = TextDraw(
      text: TextSpan(text: "40008787", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 4, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 4 + left, height));
    final text_12_3 = TextDraw(
      text: TextSpan(text: "收件：2022-03-28 14:14", style: textStyle_12),
    )
      ..layout(maxWidth: maxWidth_printer / 2, minWidth: minWidth)
      ..paint(canvas, Offset(maxWidth_printer / 2 + left, height));
    height = height + text_11_1.height + 10;
    //接下来，画剩余的直线
    //最底的横线
    canvas.drawLine(
        Offset(0.0, height), Offset(maxWidth_printer, height), paint);
    //最左的竖线
    canvas.drawLine(
        Offset(0, left), Offset(0, height), paint);
    //最右的竖线
    canvas.drawLine(
        Offset(maxWidth_printer, left), Offset(maxWidth_printer, height), paint);
    //第二条竖线
    canvas.drawLine(
        Offset(maxWidth_printer / 4, height_tuojiwu), Offset(maxWidth_printer / 4, height), paint);
    //最3条竖线
    canvas.drawLine(
        Offset(maxWidth_printer / 2, height_product), Offset(maxWidth_printer / 2, height), paint);
    //最4条竖线
    canvas.drawLine(
        Offset(maxWidth_printer * 3 / 4, height_product), Offset(maxWidth_printer * 3 / 4, height_person), paint);
  }
}
